<?php
/**
 * 站内短信 控制器
 * @author ThankPHP
 */
class Sms_mainAction extends Action
{
	/**
	 * 管理所有的站内短信
	 */
	public function AdminIndex()
	{
		EasyACL('Sms_main','AdminIndex',$_SESSION);
		$SMS = D('Sms_main');
        $count = $SMS->count(NULL,'main_id'); //计算总条数
        $listRows = 15; //每页显示数量
        $fields = 'main_id,euser_id,toeuser_id,title,sms_top,sms_state,created';//显示字段
        import('ORG.Util.Page');
        $p = new Page($count,$listRows);
        $list = $SMS->xfindall(NULL,$fields,'sms_top desc,main_id desc',$p->firstRow .','.$p->listRows);
        $page = $p->show();
        //设置到模板
        $this->assign('SMS',$list);
        $this->assign('page',$page);
        $this->display();
	}
	
	public function AdminCreate()
	{
		EasyACL('Sms_main','AdminShow',$_SESSION);
        //POST
        if ($this->isPost())
        {
            $Input = Input::getInstance();
            $Input->filter('addslashes');

            $SMS = D('Sms_main');
            $SMS->euser_id = $Input->post('euser_id');
            $SMS->toeuser_id = $Input->post('toeuser_id');
            $SMS->title = $Input->post('title');
            $SMS->content = $Input->post('content');
            $SMS->sms_top = $Input->post('sms_top');//置顶
            $SMS->sms_state = $Input->post('sms_state');//状态 是否读取
            $SMS->created = time();
            $result = $SMS->add();
            if (!$result) {
                halt('尝试发送短信失败!');
            }
            $this->redirect('AdminIndex','Sms_main');
        }
        //Get模板
        $this->display();
	}
	
	public function AdminDel($main_id)
	{
		EasyACL('Sms_main','AdminDel',$_SESSION);
        $SMS = D('Sms_main');
        $result = $SMS->deleteById($main_id,TRUE);
        if (!$result) {
            return FALSE;
        }
        return TRUE;
	}

    public function AdminDels()
    {
        EasyACL('Sms_main','AdminDels',$_SESSION);
        //POST
        if($this->isPost()) {
            //清理过期短信，如果短信在收件箱里超过2个月，将自动删除
            if($_POST['Submit'] == '清除过期短信') {
                $lastmonth = mktime(0, 0, 0, date("m")-2, date("d"),   date("Y"));
                $SMS = D('Sms_main');
                $result = $SMS->delete('created <= '.$lastmonth.' and sms_state = \'Y\'',NULL,NULL,TRUE);
                if(!$result) {
                    halt('尝试清理过期短信失败!');
                }
                $this->redirect('AdminIndex','Sms_main');
            } else {
                $main_id = $_POST['main_id'];
                $Error = '';
                foreach($main_id as $id){
                    $result = $this->AdminDel($id);
                    if(!$result) {
                        $Error .= '尝试删除编号为'.$id.'短信失败<br />';
                    }
                }
                if(empty($Error)) {
                    $this->redirect('AdminIndex','Sms_main');
                } else {
                    halt($Error);
                }
            }
        }

        //GET
        if($this->isGet()) {
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            $result = $this->AdminDel($Input->get('main_id'));
            if(!$result) {
                halt('尝试删除编号为' . $Input->get('main_id') . '短信失败!');
            }
            $this->redirect('AdminIndex','Sms_main');
        }
    }
    /**
     * 设置置顶信息
     * */
    public function AdminSetSmsTop() {
        EasyACL('Sms_main','AdminSetSmsTop',$_SESSION);
        $Input = Input::getInstance();
        $Input->filter('addslashes');
        
        //设置查询条件
        import('ORG.Util.HashMap');
        $Map = new HashMap();
        $Map->put('main_id',array('EQ',$Input->get('main_id')));
        $Static = $Input->get('Static');
        
        $SMS = D('Sms_main');
        $SMS->where($Map)->find();
        //如果当前状态是Y 则设置成N 
        $SMS->sms_top = ($Static == 'Y') ? 'N' : 'Y';
        $result = $SMS->save();
        if(!$result) {
            halt('尝试设置置顶状态失败!');
        }
        $this->redirect('AdminIndex','Sms_main');
    }
}
?>
